layui.config({
    base: CXL.ctxPath + '/externaljs/'
}).extend({ //设定组件别名
    echarts: 'echarts/echarts',
    axios: 'axios/axios'
});
layui.use([ 'table', 'laydate', 'echarts', 'axios'], function () {
    var admin = layui.admin;
    var table = layui.table;
    var form = layui.form;
    var laydate = layui.laydate;
    var echarts = layui.echarts;
    var $ = layui.$;
    var axios = layui.axios;

//执行方法
    initPage();


    //函数区域
    function initPage() {
        // initMaterialSelect();
        // apartechart = echarts.init(apartchart);
        // updateApartChartData();
        // $(document).ready(function () {
        //     apartechart.resize();
        // })
        // window.addEventListener("resize", function () {
        //     apartechart.resize();
        // });


    }


    /**
     * 表格参数
     */
    var Page = {
        tableId: 'stock-table',
        url: CXL.ctxPath + '/stock/getNowStock',
        // toolbar: '#plan-toolbar',
        // where: {field: 'endTime', order: 'desc'},
        initColumn: function () {
            return [[
                {type: 'numbers'}
                , {field: 'materialId', title: '物料编号', hide:true,width: 120}
                , {field: 'materialName', title: '物料名称', width: 120}
                , {field: 'volume', title: '体积', width: 120}
                , {field: 'weight', title: '重量', width: 120}


            ]];
        },
    };

    //导出按钮监听
    $('#exportRecord').click(function () {
        let postData = {};
        //查询字段
        postData['beginTime'] = $("#timeBegin").val();
        postData['endTime'] = $("#timeEnd").val();


        $.ajax({
            url: CXL.ctxPath + "/stock/exportFaultRecord",
            type: "POST",
            data: postData,
            dataType: "json",
            responseType: "arraybuffer",
            success: function (res) {
                if (res.success) {
                    let path = String(res.obj);
                    downloadFile(path);
                    CXL.success("成功！");

                } else {
                    CXL.error(res.msg);
                }
            },
            fail: function () {
                CXL.error("网络错误！");
            }
        });
    });
    function downloadFile(filePath) {
        console.log('asasasa');
        let param = new URLSearchParams()
        param.append('filePath', filePath);
        // param.append('pwd', 'admin')
        axios({
            url: CXL.ctxPath + "/common/faultrecord/download",
            method: 'post',
            responseType: 'blob',
            data: param
        })
            .then(res => {
                //文件名 文件保存对话框中的默认显示
                let fileName = new Date().toLocaleTimeString()+"导出记录.xlsx";
                let data = res.data;
                if (!data) {
                    return
                }
                console.log(res);
                //构造a标签 通过a标签来下载
                let url = window.URL.createObjectURL(new Blob([data]))
                let a = document.createElement('a')
                a.style.display = 'none'
                a.href = url
                //此处的download是a标签的内容，固定写法，不是后台api接口
                a.setAttribute('download', fileName)
                document.body.appendChild(a)
                //点击下载
                a.click()
                // 下载完成移除元素
                document.body.removeChild(a);
                // 释放掉blob对象
                window.URL.revokeObjectURL(url);
            })
            .catch(res => {
            })
    }


    /**
     * 页面查询
     */
    Page.searchAction = function () {
        let queryData = {};
        //查询字段
        if ($("#craneidinput").val() != 0) {
            queryData['craneId_begin'] = parseInt($("#craneidinput").val());
            queryData['craneId_end'] = parseInt($("#craneidinput").val());
        } else {
            queryData['craneId_begin'] = 1;
            queryData['craneId_end'] = 4;
        }
        queryData['endTime_begin'] = $("#timeBegin").val();
        queryData['endTime_end'] = $("#timeEnd").val();
        queryData['workType'] = $("#tasktypeinput").val();
        var materialId = parseInt($("#materialTypeSelect1").val());

        // var outputId =   parseInt($("#outportStoreSelect1").val());
        // queryData['materialId'] = parseInt($("#materialTypeSelect1").val());

        // console.log(materialId);
        if(!isNaN(materialId)){
            queryData['materialId_begin'] = materialId;
            queryData['materialId_end'] = materialId;
        } else {
            queryData['materialId_begin'] = 0;
            queryData['materialId_end'] =100;
        }



        let postData = {};
        postData['craneId'] = $("#craneidinput").val();
        postData['beginTime'] = $("#timeBegin").val();
        postData['endTime'] = $("#timeEnd").val();
        postData['workType'] = $("#tasktypeinput").val();
        updateApartChartData(postData);
        // console.log(queryData);
        table.reload(Page.tableId, {page: {curr: 1}, where: queryData});
    }

    table.render({
        elem: '#' + Page.tableId
        , url: Page.url
        // , height: 'full-140'
        , page: true
        , limit: 10
        , autoSort: false
        ,done: function (res) {
            console.log(res);
            $("#total").html(res.sumWeight);
        }
        , cols: Page.initColumn()
    });

    /**
     * 时间选择器渲染
     * 开始时间
     */
    laydate.render({
        elem: '#timeBegin' //指定元素
        , type: 'datetime'
        , value: new Date(new Date().getTime() - 60 * 60 * 24)
    });

    /**
     * 时间选择器渲染
     * 结束时间
     */
    laydate.render({
        elem: '#timeEnd' //指定元素
        , type: 'datetime'
        , value: new Date()
    });

});
